package daos;

import java.util.List;

import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import entidades.Aula;

public class DAOaula {
	public void salvar (Aula a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.persist(a);
		em.getTransaction().commit();
	em.close();
	emf.close();
		
	}
	public void alterar(Aula a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.merge(a) ;
		em.getTransaction().commit();
	em.close();
	emf.close();
	}
	public List<Aula> listar(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		Query b = (Query) em.createQuery("SELECT c FROM Aula c");
		@SuppressWarnings("unchecked")
		List<Aula> lista = (List<Aula>)((javax.persistence.Query) b).getResultList();
		em.getTransaction().commit();
		em.close();
		emf.close();
		
		return lista;
	}
}
